package cn.bluedot.controller;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.bluedot.model.BaseBody;
import cn.bluedot.model.Condition;
import cn.bluedot.model.ConditionList;
import cn.bluedot.model.Equipment;
import cn.bluedot.model.EquipmentExceptionVo;
import cn.bluedot.model.ExcCondition;
import cn.bluedot.model.MonitorVedio;
import cn.bluedot.model.SensorDataVo;
import cn.bluedot.model.TableBodyList;
import cn.bluedot.model.VideoCondition;
import cn.bluedot.model.VideoConditionList;
import cn.bluedot.service.EquMsgShowService;

/**
*
* @author 余聪
* @time:2019年3月8日下午7:20:26
*
*/

/*@RunWith(SpringJUnit4ClassRunner.class)*/
@ContextConfiguration("classpath:spring-mybatis.xml")
@Controller
@RequestMapping("equ")
public class EquMsgShowController {
	    
	@Autowired
	EquMsgShowService equMsgShowService;

	@RequestMapping("selectAllEqu")
	@ResponseBody
	public BaseBody selectAllEqu() {

		List<Equipment> equList = equMsgShowService.selectAllEqu();

		return BaseBody.success().add("equList", equList);

	}
	
	@RequestMapping("equMsgShow")
	@ResponseBody
	public BaseBody equMsgShow(@RequestBody Condition condition) {
		System.out.println("##################"+condition.getEquType());
		ConditionList conditionList = equMsgShowService.selectConditionList(condition, 1);
		TableBodyList tableBodyList = equMsgShowService.selectTableBodyList(condition, 1);
		Integer totalPage = equMsgShowService.selectTableBodyTotalPage(condition, 1);
		System.out.println("EquMsgShow()" + conditionList.getEquTypeList());
		BaseBody baseBody = BaseBody.success().add("conditionList", conditionList);
		baseBody.add("totalPage", totalPage);
		baseBody.add("tableBodyList", tableBodyList);
		return baseBody;
	}
	
	@RequestMapping("equExcShow")
	@ResponseBody
	public BaseBody equExcShow(@RequestBody ExcCondition excCondition) throws ParseException {
		System.out.println("equExcShow()");
		ConditionList conditionList = equMsgShowService.selectConditionList(excCondition, 1);
		List<EquipmentExceptionVo> equExcList = equMsgShowService.selectEquExcList(excCondition, 1);
		Integer totalPage = equMsgShowService.selectEquExcTotalPage(excCondition, 1);
		BaseBody baseBody = BaseBody.success().add("conditionList", conditionList);
		baseBody.add("totalPage", totalPage);
		baseBody.add("equExcList", equExcList);
		return baseBody;
	}
	
	@RequestMapping("getSensorData")
	@ResponseBody
	public BaseBody getSensorData(@RequestParam(value = "sensorIds[]") Integer[] sensorIds, Date startTime, Date endTime) {
		Map<Integer, List<SensorDataVo>> sensorData = equMsgShowService.selectSensorData(sensorIds,startTime,endTime);
		BaseBody baseBody = BaseBody.success().add("sensorData", sensorData);
		return baseBody;
	}

	@RequestMapping("updateEquState")
	@ResponseBody
	public BaseBody updateEquState(@RequestBody Equipment equipment) {
		System.out.println("updateEquState");
		equMsgShowService.updateEquipment(equipment);
		return BaseBody.success();
	}

	@RequestMapping("getSensorNewData")
	@ResponseBody
	public BaseBody getSensorNewData(@RequestParam(value = "sensorIds[]") Integer[] sensorIds) {
		System.out.println("getSensorNewData");
		List<Float> newDataList = equMsgShowService.getSensorNewData(sensorIds);
		return BaseBody.success().add("newDataList", newDataList);
	}

	@RequestMapping("equVideoShow")
	@ResponseBody
	public BaseBody equVideoShow(@RequestBody VideoCondition videoCondition) {
		VideoConditionList videoConditionList = equMsgShowService.selectVideoConditionList(videoCondition,1);
		List<MonitorVedio> equVideoList = equMsgShowService.selectEquVideoList(videoCondition);
		return BaseBody.success().add("videoConditionList", videoConditionList).add("equVideoList", equVideoList);
	}
	
/*	@Test
	public void test1(){
		Condition condition1 = new Condition();
		condition1.setEquType("传感器");
		condition1.setEquName("温度传感器");
		condition1.setPigstyId(1);
		TableBodyList tableBodyList = equMsgShowService.selectTableBodyList(condition1, 1);
		ConditionList conditionList = equMsgShowService.selectConditionList(condition1, 1);
		System.out.println("EquMsgShow()");
	}*/

}
